//
//  WebLinkViewController.swift
//  Wallpaper
//
//  Created by YEHWANG-iOS on 2024/10/25.
//

import UIKit
import JXPagingView
import JXSegmentedView
import MJRefresh
import ETNavBarTransparent

class WebLinkViewController: UIViewController {
    
    @IBOutlet weak var cov: UIView!
    
    lazy var paging: JXPagingSmoothView = {
        return JXPagingSmoothView(dataSource: self)
    }()
    lazy var segmentedView: JXSegmentedView = {
        return JXSegmentedView()
    }()
    lazy var headerView: WebLinkViewHeader = {
        let v = WebLinkViewHeader.fromNib
        let w = view.bounds.width
        let h = w * 1600 / 3840
        v.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: h)
        return v
    }()
    let dataSource = JXSegmentedTitleDataSource()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navBarBgAlpha = 1.0
        self.navigationItem.title = "WeTab"
        
        view.backgroundColor = .white
        cov.addSubview(paging)
        cov.clipsToBounds = true

        dataSource.titles = ["精选图片", "动态壁纸", "渐变背景"]
        dataSource.titleSelectedColor = UIColor.systemPurple
        dataSource.titleNormalColor = UIColor.black
        dataSource.isTitleColorGradientEnabled = true
        dataSource.isTitleZoomEnabled = false

        segmentedView.backgroundColor = .white
        segmentedView.isContentScrollViewClickTransitionAnimationEnabled = false
        segmentedView.delegate = self
        segmentedView.dataSource = dataSource

        let line = JXSegmentedIndicatorLineView()
        line.indicatorColor = UIColor.systemPurple
        line.verticalOffset = 10
        line.indicatorHeight = 1.8
        line.indicatorCornerRadius = 0
        segmentedView.indicators = [line]

        headerView.clipsToBounds = true
        headerView.contentMode = .scaleAspectFill

        segmentedView.contentScrollView = paging.listCollectionView

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        paging.snp.makeConstraints { make in
            make.edges.equalTo(cov)
        }
    }
}

extension WebLinkViewController: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        navigationController?.interactivePopGestureRecognizer?.isEnabled = (index == 0)
    }
}

extension WebLinkViewController: JXPagingSmoothViewDataSource {
    func heightForPagingHeader(in pagingView: JXPagingSmoothView) -> CGFloat {
        let w = view.bounds.width
        let h = w * 1600 / 3840
        return h
    }

    func viewForPagingHeader(in pagingView: JXPagingSmoothView) -> UIView {
        return headerView
    }

    func heightForPinHeader(in pagingView: JXPagingSmoothView) -> CGFloat {
        return 50
    }

    func viewForPinHeader(in pagingView: JXPagingSmoothView) -> UIView {
        return segmentedView
    }

    func numberOfLists(in pagingView: JXPagingSmoothView) -> Int {
        return dataSource.titles.count
    }

    func pagingView(_ pagingView: JXPagingSmoothView, initListAtIndex index: Int) -> JXPagingSmoothViewListViewDelegate {
        let vc = WebLinkCategoryController.fromStoryboard(.main)
        return vc
    }
}


